<?php

/** Category.php
 *栏目管理
 *@author huwhois
 *@time 2017/11/17
*/
namespace app\admin\controller;

use app\admin\model\Category as CategoryModel;
use think\Request;
use think\Db;

class Category extends Base
{
    protected $category_model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->category_model = new CategoryModel();
    }

    public function index()
    {
        $data = $this->category_model->select();

        $data = obj_tree($data);
        
        // $this->assign('type', config('type'));
        $this->assign("data", $data);
        return $this->fetch();
    }

    public function add($tid = null)
    {
        if (Request::instance()->isPOST()) {
            $param = input('');
            // var_dump($param);
            // exit;
            $name = trim($param['name']);

            if ($name == '') {
                $this->error("栏目名不能为空");
            }
            
            $pid = intval($param['pid']);
            $type = intval($param['type']);
            $sort = intval($param['sort']);
            $desc = $param['description'];
            $content = isset($param['editorValue']) ? $param['editorValue'] : '';
            $time = time();

            try {
                $this->category_model->save([
                    'pid' => $pid,
                    'name' => $name,
                    'type' => $type,
                    'sort' => $sort,
                    'desc' => $desc,
                    'content' => $content,
                    'mtime' => $time
                ]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:".$msg);
            }

            $this->success(config('msg.1003'), 'index');
        } else {
            $top = $this->category_model->where('pid', 0)->column('id, name');

            $this->assign('top', $top);
            // $this->assign('type', config('type'));
            $this->assign('tid', $tid);
            return $this->fetch();
        }
    }

    public function edit($id = null)
    {
        $data = $this->category_model->get($id);
        
        if (Request::instance()->isPOST()) {
            $param = input('');
            // var_dump($param);
            // exit;
            $name = trim($param['name']);

            if ($name == '') {
                $this->error("栏目名不能为空");
            }
            
            $pid = intval($param['pid']);
            // $type = intval($param['type']);
            $sort = intval($param['sort']);
            $status = intval($param['status']);
            $desc = $param['description'];
            $content = isset($param['editorValue']) ? $param['editorValue'] : '';
            $time = time();

            try {
                $this->category_model->save([
                    'pid' => $pid,
                    'name' => $name,
                    // 'type' => $type,
                    'sort' => $sort,
                    'status' => $status,
                    'desc' => $desc,
                    'content' => $content,
                    'mtime' => $time
                ], ['id'=>$id]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:".$msg);
            }

            $this->success(config('msg.1003'), 'index');
        } else {
            $top = $this->category_model->where('pid', 0)->column('id, name');

            $this->assign('top', $top);
            $this->assign('type', config('type'));
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    public function del($id)
    {
        if (Request::instance()->isAjax()) {
            $result = $this->category_model->where('pid', 'IN', $id)->column('id');
            if ($result) {
                $this->error('子栏目不为空, 删除出错');
            }

            $result = model('article')->where('cid', 'IN', $id)->column('id');
            if ($result) {
                return ['code'=>0, 'msg'=>'栏目下文章不为空, 不可删除'];
            }

            $result = model('product')->where('cid', 'IN', $id)->column('id');
            if ($result) {
                return ['code'=>0, 'msg'=>'栏目下产品不为空, 不可删除'];
            }

            $res = $this->category_model->destroy($id);
            
            if (!$res) {
                return ['code'=>0, 'msg'=>config('msg.failed')];
            } else {
                return ['code'=>2, 'msg'=>config('msg.success')];
            }
        }
    }
}
